package ca.usherbrooke.domus.sipnmove.call.view.impl;

import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import ca.usherbrooke.domus.sipnmove.call.impl.SipCallListener;
import ca.usherbrooke.domus.sipnmove.call.impl.SocketCallListener;
import ca.usherbrooke.domus.sipnmove.call.view.CallView;
import ca.usherbrooke.domus.sipnmove.useragent.controller.ActionController;

/**
 * View for SIP/Socket calls
 * 
 * This view is implemented for specific case of SIP calls over Socket calls.
 * 
 * TODO : Should be implemented via a decorator/decorated organization.
 */
@SuppressWarnings("serial")
public class SipSocketCallView extends JFrame implements CallView, SipCallListener, SocketCallListener, WindowListener {

	/**
	 * Content text area
	 */
	private JTextArea contentTextArea;

	/**
	 * Content text area
	 */
	private MessageTextField messageTextField;

	/**
	 * Action controller for message sending
	 */
	private ActionController sendMessageActionController;

	/**
	 * Action controller for message sending
	 */
	private ActionController terminateCallActionController;
	
	/**
	 * Default constructor
	 */
	public SipSocketCallView() {
		super("SIPnMove - Call");
	}

	@Override
	public void processChangedState() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processReceivedMessage(String message) {
		contentTextArea.append("Him: " + message + "\n");
	}

	@Override
	public void processSentMessage(String message) {
		contentTextArea.append("Me: " + message + "\n");
	}

	@Override
	public void display() {
		
		this.setSize(300, 400);

		this.getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		JLabel contactLabel = new JLabel("sip:contact@contact");
		contactLabel.setPreferredSize(new Dimension(300, 30));
		this.getContentPane().add(contactLabel);

		contentTextArea = new JTextArea(">>> Sending invitation...\n");
		contentTextArea.setPreferredSize(new Dimension(300, 300));
		contentTextArea.setEditable(false);
		this.getContentPane().add(contentTextArea);

		messageTextField = new MessageTextField();
		messageTextField.setPreferredSize(new Dimension(300, 30));
		messageTextField.setActionController(sendMessageActionController);
		this.getContentPane().add(messageTextField);

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(this);

		this.pack();
		this.setVisible(true);

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		terminateCallActionController.doAction();
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return the contentTextArea
	 */
	public JTextArea getContentTextArea() {
		return contentTextArea;
	}

	/**
	 * @param contentTextArea the contentTextArea to set
	 */
	public void setContentTextArea(JTextArea contentTextArea) {
		this.contentTextArea = contentTextArea;
	}

	/**
	 * @return the sendMessageActionController
	 */
	public ActionController getSendMessageActionController() {
		return sendMessageActionController;
	}

	/**
	 * @param sendMessageActionController the sendMessageActionController to set
	 */
	public void setSendMessageActionController(ActionController sendMessageActionController) {
		this.sendMessageActionController = sendMessageActionController;
	}


	/**
	 * @return the messageTextField
	 */
	public MessageTextField getMessageTextField() {
		return messageTextField;
	}

	/**
	 * @param messageTextField the messageTextField to set
	 */
	public void setMessageTextField(MessageTextField messageTextField) {
		this.messageTextField = messageTextField;
	}

	/**
	 * @return the terminateCallActionController
	 */
	public ActionController getTerminateCallActionController() {
		return terminateCallActionController;
	}

	/**
	 * @param terminateCallActionController the terminateCallActionController to set
	 */
	public void setTerminateCallActionController(ActionController terminateCallActionController) {
		this.terminateCallActionController = terminateCallActionController;
	}

}
